package cn.tedu.hospitalqd.service.impl;

import cn.tedu.hospitalqd.ex.ServiceException;
import cn.tedu.hospitalqd.mapper.PatientMapper;
import cn.tedu.hospitalqd.pojo.dto.PatientAddNewDTO;
import cn.tedu.hospitalqd.pojo.entity.Patient;
import cn.tedu.hospitalqd.pojo.vo.PatientListItemVO;
import cn.tedu.hospitalqd.pojo.vo.PatientStandarVO;
import cn.tedu.hospitalqd.service.PatientService;
import cn.tedu.hospitalqd.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientMapper patientMapper;

    @Override
    public void addNew(PatientAddNewDTO patientAddNewDTO) {
        // 从参数对象中获取患者名称
        String name = patientAddNewDTO.getPName();
        // 调用albumMapper的int countByName(String name)根据名称执行统计
        int countByName = patientMapper.countByName(name);
        // 判断统计结果是否大于0
        if (countByName > 0) {
            // 是：抛出异常
            String message = "添加患者失败，患者名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
    }
        Patient patient = new Patient();
        // 调用BeanUtils.copyProperties()将参数对象的属性值复制到Album对象中
        BeanUtils.copyProperties(patientAddNewDTO, patient);
        // 调用albumMapper的int insert(Album album)方法将相册数据插入到数据库
        int rows = patientMapper.insert(patient);
        if (rows != 1) {
            String message = "添加患者失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }


    @Override
    public void delete(Long id) {

    }

    @Override
    public PatientStandarVO getStandardById(Long id) {
        return null;
    }

    @Override
    public List<PatientListItemVO> list() {
        return null;
    }
}
